﻿using System.Data;
using System.Data.SQLite;
using Castle.ActiveRecord;
using Castle.Services.Transaction;
using NHibernate;

namespace ARATMSample {
    [Transactional]
    public class TransactionalService {
        private readonly ISessionFactory sessionFactory;

        public TransactionalService(ISessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }

        [Transaction]
        public virtual void DoSomething() {
            ActiveRecordMediator<Person>.Save(new Person {
                Name = "Pepe",
            });
            IDbCommand cmd = new SQLiteCommand();
            using (var session = sessionFactory.OpenSession()) {
                cmd.Connection = session.Connection;
                session.Transaction.Enlist(cmd);
            }
            cmd.CommandText = "delete from person";
            cmd.ExecuteNonQuery();
        }
    }
}